package com.coocaa.ops.api.v1.content.service.impl;

import com.coocaa.ops.api.common.model.HeaderModel;
import com.coocaa.ops.api.v1.content.cache.ITabInfoCache;
import com.coocaa.ops.api.v1.content.model.internal.TabContentInfo;
import com.coocaa.ops.api.v1.content.model.output.TabContent;
import com.coocaa.ops.api.v1.content.model.query.TabQueryModel;
import com.coocaa.ops.api.v1.content.service.ITabService;
import com.coocaa.ops.api.v1.content.service.business.INormalTabService;
import com.coocaa.ops.api.v1.content.util.TabContentQueryUtil;
import com.coocaa.ops.api.v1.content.util.TabContentResultUtil;
import com.coocaa.ops.api.v1.content.util.TabTypeValidator;
import com.coocaa.ops.api.v1.policy.model.PolicyModel;
import com.coocaa.ops.api.v1.policy.service.IPolicyService;
import com.coocaa.ops.common.core.biz.tab.entity.TabInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zen
 * @date 2020-06-24 11:36
 * @description
 */
@Service
public class TabServiceImpl implements ITabService {

    @Autowired
    private IPolicyService policyService;

    @Autowired
    private ITabInfoCache tabInfoCache;

    @Autowired
    private INormalTabService normalTabService;

    @Override
    public TabContent getTabContent(HeaderModel header, TabQueryModel tabQueryModel) {
        /**
         * 逻辑：
         * 获取策略类型
         * 获取版面信息，判断版面类型
         * 普通：
         *      获取关联的板块id
         *      筛选板块id
         *      根据板块id获取板块内容
         * 视频流：
         *
         */
        PolicyModel policyModel = policyService.getPolicyByHeader(header);
        tabQueryModel = TabContentQueryUtil.buildTabQueryModel(tabQueryModel, header, policyModel);
        return getTabContent(tabQueryModel);
    }

    private TabContent getTabContent(TabQueryModel tabQueryModel) {
        Integer tabId = Integer.valueOf(tabQueryModel.getTabId());
        TabInfoEntity tab = tabInfoCache.getTabInfo(tabId, tabQueryModel.getPolicyCategory());
        if (tab == null) {
            return null;
        }
        tabQueryModel.setHasDmp(tab.getHasDmp());
        if (TabTypeValidator.isNormalTab(tab.getTabType())) {
            TabContentInfo tabContentInfo = normalTabService.getNormalTabContent(tabQueryModel);
            return TabContentResultUtil.buildNormalTabContentResult(tabContentInfo, tab);
        } else if (TabTypeValidator.isVideoStreamTab(tab.getTabType())) {
            //TODO:
            return null;
        } else if (TabTypeValidator.isPosterTab(tab.getTabType())) {
            //TODO:
            return null;
        }
        return null;
    }
}
